package com.han.spider.cloudmeeting;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Hanjiafeng
 * @Date: 2020/6/29
 * @Desc:
 */
public class MeetingPool implements Runnable {

    private final String userId;
    private final String password;

    public MeetingPool(String userId, String password) {
        this.userId = userId;
        this.password = password;
    }

    @Override
    public void run() {
        try {
            operateMeeting(0);
        } catch (IOException | InterruptedException | URISyntaxException e) {
            e.printStackTrace();
        }
    }

    /**
     * 操作会议
     *
     * @param flag 0:创建会议,1:删除所有会议
     * @throws IOException          异常
     * @throws InterruptedException 异常
     * @throws URISyntaxException   异常
     */
    private void operateMeeting(int flag) throws IOException, InterruptedException, URISyntaxException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1), r -> new Thread(r, userId + "-beat"));
        WebSiteLogin webSiteLogin = new WebSiteLogin(userId, password);
        String wsUrl = webSiteLogin.login();
        MeetingManager manager = new MeetingManager(userId);
        manager.connect(wsUrl);
        manager.getUserInfo();
        if (flag == 0) {
            manager.startMeeting();
        } else if (flag == 1) {
            manager.endMeeting();
        }
        executor.execute(new HeartTimer(manager.getClient(), 60 * 11, userId));
        executor.shutdown();
    }
}
